#include <iostream>
using namespace std;
#include <cstring>
#include <windows.h>

#include "Utils.hpp"
#include "base64.hpp"
#include "EventHandler.hpp"

int Button_Click(wchar_t* fname,wchar_t* methon_raw,wchar_t* para_raw){
	int err;
	char path[255];
	lua_State *L = Lua_Init();

	char *para;

	para = UnicodeToGBK(para_raw);

	char *methon = UnicodeToGBK(methon_raw);

	strcpy(path,"/Program Files/wmssystem/Scripts/GUI/EventHandler/Dispatcher.lua");
	//sprintf(path,"/Program Files/wmssystem/Scripts/GUI/EventHandler/%s.lua",UnicodeToGBK(fname));
	printf("DBG:I am trying to load&execute EventHandler \"%s\" from file \"%s\" with para \"%s\"\n",methon,path,para);
	err = luaL_dofile(L,path);
	if(err){
		printf("ERR MSG:Error while loading EventHandler file %s\n",lua_tostring(L,-1));
		lua_pop(L,1);
	}
	printf("DBG:EventHandler lua file loaded.\n");
	lua_getglobal(L,"Dispatcher");
	
	lua_pushstring(L,UnicodeToGBK(fname));
	lua_pushstring(L,methon);
	lua_pushstring(L,para);
	err = lua_pcall(L,3,0,0);
	
	if(err){
		printf("DBG:Error while executing EventHandler function.\n");
		printf("ERR MSG:%s\n",lua_tostring(L,-1));
		lua_pop(L,1);
	}

	lua_close(L);
	return 1;
}